#include "setting.h"


#include "NTPClient.h" 
#include "JsonListener.h"




lv_obj_t *label_time;
 // Set offset time in seconds to adjust for your timezone, for example:
  // GMT +1 = 3600
  // GMT +8 = 28800
  // GMT -1 = -3600
  // GMT 0 = 0
NTPClient timeClient("ntp.aliyun.com",28800);//+8区，60*60*8

#define NTP_SERVERS "ntp.aliyun.com", "ntp1.aliyun.com", "s1c.time.edu.cn"
// WIFI

#define UTC_OFFSET              8       // (utc+) TZ in hours
#define DST_MN          0      // use 60mn for summer time in some countries
// Setup
const int UPDATE_INTERVAL_SECS = 20 * 60; // Update every 20 minutes


// OpenWeatherMap Settings
// Sign up here to get an API key:
// https://docs.thingpulse.com/how-tos/openweathermap-key/
String OPEN_WEATHER_MAP_APP_ID = "26adb3d2c8880174ba4ac70d0a1c8906";
/*
Go to https://openweathermap.org/find?q= and search for a location. Go through the
result set and select the entry closest to the actual location you want to display 
data for. It'll be a URL like https://openweathermap.org/city/2657896. The number
at the end is what you assign to the constant below.
 */
String OPEN_WEATHER_MAP_LOCATION_ID = "1816670";

// Pick a language code from this list:
// Arabic - ar, Bulgarian - bg, Catalan - ca, Czech - cz, German - de, Greek - el,
// English - en, Persian (Farsi) - fa, Finnish - fi, French - fr, Galician - gl,
// Croatian - hr, Hungarian - hu, Italian - it, Japanese - ja, Korean - kr,
// Latvian - la, Lithuanian - lt, Macedonian - mk, Dutch - nl, Polish - pl,
// Portuguese - pt, Romanian - ro, Russian - ru, Swedish - se, Slovak - sk,
// Slovenian - sl, Spanish - es, Turkish - tr, Ukrainian - ua, Vietnamese - vi,
// Chinese Simplified - zh_cn, Chinese Traditional - zh_tw.
String OPEN_WEATHER_MAP_LANGUAGE = "en";
const uint8_t MAX_FORECASTS = 4;

const boolean IS_METRIC = true;

// Adjust according to your language
const String WDAY_NAMES[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
const String MONTH_NAMES[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};



 






static void time_create(void);
void setting_create(lv_obj_t *obj, lv_event_t event)
{
    
 
    if(event==LV_EVENT_CLICKED)
    {
        time_create();
        eink_scene_ready=false;
        eink_flash_ready=false;

    }
}

static void btn_back_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(event == LV_EVENT_CLICKED) {
        lv_obj_t * par = lv_obj_get_parent(btn);
        lv_obj_del(par);
        lv_obj_del(label_time);
        eink_scene_ready=false;
        eink_flash_ready=false;
        WiFi.mode(WIFI_OFF); 
        lv_obj_set_hidden(label_menu,false);
    }
}

// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
static void my_task(lv_task_t * task)
{
 
  io.digitalWrite0(3,0);
  tft.setTextColor(TFT_BLUE,TFT_WHITE);
  /*Do something with LittlevGL*/
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  int counter = 0;
  while (WiFi.status() != WL_CONNECTED) {
      
    delay(500);
    tft.drawNumber(counter,100,0);
    counter++;
    if(counter>=50)    break;
  }
  tft.println("WiFi connected.");
  tft.println("IP address: ");
  tft.println(WiFi.localIP());
  timeClient.begin();
 
  timeClient.forceUpdate();
 
  // The formattedDate comes with the following format:
  // 2018-05-28T16:00:13Z
  // We need to extract date and time
  formattedDate = timeClient.getFormattedDate();
  tft.println(formattedDate);
  Serial.println(formattedDate);

  int splitT = formattedDate.indexOf("T");
  dayStamp = formattedDate.substring(0, splitT);
  timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);

 

  rtc.adjust(DateTime(dayStamp.c_str(), timeStamp.c_str()));

  Serial.printf("%s\n",dayStamp.c_str());  Serial.println("");
  Serial.printf("%s\n",timeStamp.c_str());  Serial.println("");

 

  Serial.println("ok");
  WiFi.mode(WIFI_OFF);
  tft.println("wifi off");
  io.digitalWrite0(3,1);  
    
}

static void btn_sync_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(event == LV_EVENT_RELEASED) {
         //连接wifi，并进行时间同步
         static uint32_t user_data = 10;
            lv_task_t * task = lv_task_create(my_task, 500, LV_TASK_PRIO_MID, &user_data);
          lv_task_once(task);
        
        lv_label_set_text(label_menu,"sync");

    }
}

static void lv_ex_btnm_1(lv_obj_t *par);
static void time_create(void)
{

    //tft
    lv_obj_set_hidden(label_menu,true);
    

    label_time = lv_label_create(scr_tft,NULL);
    DateTime now = rtc.now();
    char buf[100];
    strncpy(buf,"YYYY.MM.DD  hh:mm:ss\0",100);
    lv_label_set_text(label_time,now.format(buf));
    lv_obj_align(label_time,scr_tft,LV_ALIGN_CENTER,0,0);


  //eink
    lv_obj_t *scr = lv_obj_create(scr_eink,NULL);
    lv_obj_set_size(scr,176,264);



    lv_ex_btnm_1(scr);
    // lv_obj_t *btn_back = lv_btn_create(scr,NULL);
    // lv_obj_set_size(btn_back,50,50);
    // lv_obj_set_pos(btn_back,0,0);
    // lv_obj_set_event_cb(btn_back, btn_back_event_cb); 
    
    // lv_obj_t *label_back = lv_label_create(btn_back,NULL);
    // lv_label_set_text(label_back,LV_SYMBOL_LEFT);
    // lv_obj_align(label_back,btn_back,LV_ALIGN_CENTER,0,0);


    // LV_IMG_DECLARE(img_sync_48);


    // lv_obj_t *btn_sync = lv_imgbtn_create(scr,NULL);
    // lv_imgbtn_set_src(btn_sync, LV_BTN_STATE_REL,&img_sync_48);
    // lv_obj_set_pos(btn_sync,0,100);



    // lv_obj_set_event_cb(btn_sync, btn_sync_event_cb); 



}

static  void event_handler(lv_obj_t * obj, lv_event_t event)
{
  if(event == LV_EVENT_VALUE_CHANGED) 
  {
    const char * txt = lv_btnm_get_active_btn_text(obj);
    if(txt==LV_SYMBOL_LEFT)
    {
        lv_obj_t * par = lv_obj_get_parent(obj);
        lv_obj_del(par);
        lv_obj_del(label_time);
        eink_scene_ready=false;
        eink_flash_ready=false;
        WiFi.mode(WIFI_OFF); 
        lv_obj_set_hidden(label_menu,false);
      
    }
    else if(txt == LV_SYMBOL_REFRESH )
    {
       //连接wifi，并进行时间同步
        static uint32_t user_data = 10;
        lv_task_t * task = lv_task_create(my_task, 500, LV_TASK_PRIO_MID, &user_data);
        lv_task_once(task);

        lv_label_set_text(label_menu,"sync");
    }
  }
}

static const char * btnm_map[] = {LV_SYMBOL_LEFT,LV_SYMBOL_REFRESH, ""};

static void lv_ex_btnm_1(lv_obj_t *par)
{
    lv_obj_t *btnm1 = lv_btnm_create(par, NULL);
    lv_btnm_set_map(btnm1, btnm_map);
    lv_btnm_set_btn_width(btnm1, 0, 1);        /*Make "Action1" twice as wide as "Action2"*/
	  lv_btnm_set_btn_width(btnm1, 1, 1);        /*Make "Action1" twice as wide as "Action2"*/
    lv_obj_set_size(btnm1,176,50);
    lv_obj_align(btnm1, par, LV_ALIGN_IN_TOP_MID, 0, 0);
    lv_obj_set_event_cb(btnm1, event_handler);
}